import 'package:FiduMart/screen/home/memo_page.dart';
import 'package:FiduMart/screen/webview/web_controller.dart';
import 'package:flutter/material.dart';
import 'package:FiduMart/localization/app_export.dart';
import 'package:FiduMart/screen/home/page1.dart';
import 'package:FiduMart/screen/home/page2.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../page1/page1_controller.dart';

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final controller = Get.put(Page1Controller());

  var backTime = 0;

  @override
  void initState() {
    super.initState();
  }
  var currentIndex = 0.obs;

  @override
  Widget build(BuildContext context) {
    return SafeArea(child: iosView(context));
  }

  Widget iosView(BuildContext context) {
    return FutureBuilder(
      future: controller.checkApp(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return Center(
                child:
                    Text("${snapshot.error}", style: TextStyle(fontSize: 20,color: Colors.white)));
          } else {
            return controller.passAudit() ? buildHomeBody() : MemoPage();
          }
        } else {
          return Center(child: CircularProgressIndicator());
        }
      },
    );
  }

  Widget buildHomeBody() {
    return WillPopScope(
        onWillPop: () async {
          if(await controller.webController.wvController.canGoBack()){
            controller.webController.wvController.goBack();
          }else{
            // h5未实现此方法，我这里原生做处理
            // controller.webController.wvController.runJavaScript("onBack();");
            if(DateTime.now().millisecondsSinceEpoch - backTime < 2000){
              return true;
            }
            backTime = DateTime.now().millisecondsSinceEpoch;
          }
            return false;
        },
        child: WebViewWidget(controller: controller.webController.wvController));

      // floatingActionButton: favoriteButton(),


      Scaffold(
        resizeToAvoidBottomInset: false, //键盘兼容
        body: Obx(
          () => IndexedStack(
            index: currentIndex.value,
            children:
                [Page1(),Page2()]
          ),
        ),
        bottomNavigationBar: Container(
            decoration: BoxDecoration(
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.1),
                  spreadRadius: 1,
                  blurRadius: 5,
                  offset: Offset(0, -3), // 阴影的偏移量，这里是向上偏移3个逻辑像素
                ),
              ],
            ),
            child: Obx(() => BottomNavigationBar(
              currentIndex: currentIndex.value,
              onTap: (int index) {
                currentIndex.value = index;
              },
              showUnselectedLabels: true,
              backgroundColor: Theme.of(context).colorScheme.inversePrimary,
              selectedItemColor: ColorConstant.colorFF6666FF,
              unselectedItemColor: ColorConstant.colorFF828A99,
              type: BottomNavigationBarType.fixed,
              items:
                   [
                    BottomNavigationBarItem(
                        icon: Image(
                          image: AssetImage('assets/images/homebar_quotes.png'),
                          width: 30,
                          height: 30,
                        ),
                        activeIcon: Image(
                            image: AssetImage('assets/images/homebar_quotes_a.png'),
                            width: 30,
                            height: 30),
                        label: 'home_quotes'.tr),
                    BottomNavigationBarItem(
                        icon: Image(
                          image: AssetImage('assets/images/homebar_information.png'),
                          width: 30,
                          height: 30,
                        ),
                        activeIcon: Image(
                            image: AssetImage('assets/images/homebar_information_a.png'),
                            width: 30,
                            height: 30),
                        label: 'home_Information'.tr),
                  ]
            ))));

  }
}
